package com.webapp.org.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.webapp.org.bean.DepartmentBean;
import com.webapp.org.interfaces.IDepartment;
import com.webapp.org.util.condatabase.HBConfigure;

public class DepartmentDAO implements IDepartment
{
	private SessionFactory sf = HBConfigure.getSessionFactory();
	private boolean result = false;

	/* Method to insert an Department from the records */
	public boolean insertDepartment(DepartmentBean DepartmentBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.persist(DepartmentBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception exception)
		{
			session.getTransaction().rollback();
			System.out.println("Error occured while inserting");
		} finally
		{
			session.close();
		}
		return result;
	}

	/* Method to update an Department from the records */
	public boolean updateDepartment(DepartmentBean departmentBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.update(departmentBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Exception took place in update");
		} finally
		{
			session.close();
		}
		return result;
	}

	/* Method to DELETE an Department from the records */
	public boolean deleteDepartment(int DepartmentId)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{

			DepartmentBean DepartmentBean = (DepartmentBean) session.get(
					DepartmentBean.class, DepartmentId);
			session.delete(DepartmentBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("error while deleting");
		} finally
		{
			session.close();
		}
		return result;
	}

	/* Method to retrieve an Department from the records */
	public List<DepartmentBean> retrieveDepartment()
	{
		List<DepartmentBean> DepartmentList = null;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			DepartmentList = session.createQuery("FROM DepartmentBean").list();
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error while retriving Department data");
		} finally
		{
			session.close();
		}
		return DepartmentList;
	}
	
	/* Method to retrieve an Department from the records */
	public DepartmentBean getDepartment(int departmentId)
	{
		DepartmentBean department = null;
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			 department = (DepartmentBean)session.get(DepartmentBean.class, departmentId);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error while retriving Department data");
		} finally
		{
			session.close();
		}
		return department;
	}

}
